﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Forms
{
    public partial class add_new_account_form : Form
    {
        private eCompany.eObj.currency_list m_cl = null;
        private eCompany.eObj.company m_company = null;
        private eCompany.eObj.organisation m_org = null;

        public add_new_account_form(eCompany.eObj.company comp, eCompany.eObj.currency_list cl)
        {
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;
            m_cl = cl;
            m_company = comp;
            InitCurrencyCB();
        }

        public add_new_account_form(eCompany.eObj.organisation org, eCompany.eObj.currency_list cl)
        {
            InitializeComponent();
            m_cl = cl;
            m_org = org;
            InitCurrencyCB();
        }


        private void InitCurrencyCB()
        {
            currencyComboBoxExtended.ItemHeight = currencyImageList.ImageSize.Height;
            foreach (eCompany.eObj.currency cur in m_cl.currencies)
            {
                Image img = Tools.GetCurrencyImage(cur.short_name);
                if(img != null)
                    currencyImageList.Images.Add(cur.short_name, img);
                ComboBoxExtendedItem item = new ComboBoxExtendedItem();
                item.Obj = cur.short_name;
                item.ImageTag = cur.short_name;
                currencyComboBoxExtended.Items.Add(item);
            }
        }

        private void addCmd_Click(object sender, EventArgs e)
        {
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
            string baseAccountValue = accountBaseValueTxt.Text.Replace(",", ".");

            double dBaseAccountValue = Convert.ToDouble(baseAccountValue, culture);

            eCompany.eManager.eManager eM = new eManager.eManager();
            if (m_company != null)
                eM.addAccount(m_company, currencyComboBoxExtended.Text, dBaseAccountValue);
            else
                eM.addAccount(m_org, currencyComboBoxExtended.Text, dBaseAccountValue);
            this.Close();
        }

        private void currencyComboBoxExtended_SelectedIndexChanged(object sender, EventArgs e)
        {
            validateForm();
        }

        private void validateForm()
        {
            double dbl = 0;
            if (currencyComboBoxExtended.Text != "" && double.TryParse(accountBaseValueTxt.Text, out dbl) == true)
                addCmd.Enabled = true;
            else
                addCmd.Enabled = false;
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.accountBaseValueTxt = new System.Windows.Forms.TextBox();
            this.currencyComboBoxExtended = new eCompany.ComboBoxExtended();
            this.currencyImageList = new System.Windows.Forms.ImageList(this.components);
            this.addCmd = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(10, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(97, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Account currency :";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(10, 42);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(111, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Account base value : ";
            // 
            // accountBaseValueTxt
            // 
            this.accountBaseValueTxt.Location = new System.Drawing.Point(127, 39);
            this.accountBaseValueTxt.Name = "accountBaseValueTxt";
            this.accountBaseValueTxt.Size = new System.Drawing.Size(121, 20);
            this.accountBaseValueTxt.TabIndex = 2;
            this.accountBaseValueTxt.Text = "0";
            this.accountBaseValueTxt.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.accountBaseValueTxt.TextChanged += new System.EventHandler(this.accountBaseValueTxt_TextChanged);
            // 
            // currencyComboBoxExtended
            // 
            this.currencyComboBoxExtended.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
            this.currencyComboBoxExtended.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.currencyComboBoxExtended.FormattingEnabled = true;
            this.currencyComboBoxExtended.ImageList = this.currencyImageList;
            this.currencyComboBoxExtended.Location = new System.Drawing.Point(127, 12);
            this.currencyComboBoxExtended.Name = "currencyComboBoxExtended";
            this.currencyComboBoxExtended.Size = new System.Drawing.Size(121, 21);
            this.currencyComboBoxExtended.TabIndex = 3;
            this.currencyComboBoxExtended.SelectedIndexChanged += new System.EventHandler(this.currencyComboBoxExtended_SelectedIndexChanged);
            // 
            // currencyImageList
            // 
            this.currencyImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
            this.currencyImageList.ImageSize = new System.Drawing.Size(16, 16);
            this.currencyImageList.TransparentColor = System.Drawing.Color.Transparent;
            // 
            // addCmd
            // 
            this.addCmd.Enabled = false;
            this.addCmd.Location = new System.Drawing.Point(13, 65);
            this.addCmd.Name = "addCmd";
            this.addCmd.Size = new System.Drawing.Size(235, 23);
            this.addCmd.TabIndex = 4;
            this.addCmd.Text = "Add";
            this.addCmd.UseVisualStyleBackColor = true;
            this.addCmd.Click += new System.EventHandler(this.addCmd_Click);
            // 
            // add_new_account_form
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(260, 103);
            this.Controls.Add(this.addCmd);
            this.Controls.Add(this.currencyComboBoxExtended);
            this.Controls.Add(this.accountBaseValueTxt);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "add_new_account_form";
            this.Text = "Add new Account";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void accountBaseValueTxt_TextChanged(object sender, EventArgs e)
        {
            validateForm();
        }
    }
}
